package com.qujia.station.service.impl;

import com.qujia.common.utils.DateUtils;
import com.qujia.station.domain.InventoryReportDaily;
import com.qujia.station.domain.InventoryReportMonthly;
import com.qujia.station.mapper.InventoryReportDailyMapper;
import com.qujia.station.mapper.InventoryReportMonthlyMapper;
import com.qujia.station.service.InventoryReportMonthlyService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service("inventoryReportMonthlyService")
@Slf4j
public class InventoryReportMonthlyServiceImpl implements InventoryReportMonthlyService {

    @Autowired
    private InventoryReportMonthlyMapper inventoryReportMonthlyMapper;

    @Autowired
    private InventoryReportDailyMapper inventoryReportDailyMapper;

    @Override
    public List<InventoryReportMonthly> selectInventoryReportMonthlyList(InventoryReportMonthly inventoryReportMonthly) {
        setSelectDate(inventoryReportMonthly);
        return inventoryReportMonthlyMapper.selectInventoryReportMonthlyList(inventoryReportMonthly);
    }

    @Override
    public InventoryReportMonthly inventoryReportMonthlySum(InventoryReportMonthly inventoryReportMonthly) {
        setSelectDate(inventoryReportMonthly);
        return inventoryReportMonthlyMapper.inventoryReportMonthlySum(inventoryReportMonthly);
    }

    private void setSelectDate(InventoryReportMonthly inventoryReportMonthly){
        if(Objects.isNull(inventoryReportMonthly) || Objects.isNull(inventoryReportMonthly.getStartMonth())
                || Objects.isNull(inventoryReportMonthly.getEndMonth())){
            return;
        }
        Date monthStartTime = DateUtils.getMonthStartTime(inventoryReportMonthly.getStartMonth());
        inventoryReportMonthly.setBeginTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,monthStartTime));
        Date monthEndTime = DateUtils.getMonthEndTime(inventoryReportMonthly.getEndMonth());
        inventoryReportMonthly.setEndTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,monthEndTime));
    }

    @Override
    public void inventoryReportMonthlyStatistics() {
        String upMonthStartTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.getUpMonthStartTime());
        String upMonthEndTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.getUpMonthEndTime());
        log.info("月报表定时任务开始，upMonthStartTime:[{}],upMonthEndTime:[{}]",upMonthStartTime,upMonthEndTime);
        InventoryReportDaily inventoryReportDaily = new InventoryReportDaily();
        inventoryReportDaily.setBeginTime(upMonthStartTime);
        inventoryReportDaily.setEndTime(upMonthEndTime);
        List<InventoryReportDaily> inventoryReportDailies = inventoryReportDailyMapper.selectInventoryReportDailyForMonthly(inventoryReportDaily);
        if(CollectionUtils.isEmpty(inventoryReportDailies)){
            return;
        }
        for (InventoryReportDaily reportDaily : inventoryReportDailies) {
            InventoryReportMonthly reportMonthly = new InventoryReportMonthly();
            BeanUtils.copyProperties(reportDaily,reportMonthly);
            inventoryReportMonthlyMapper.insertSelective(reportMonthly);
        }


    }
}
